package api

import (
	"fmt"
	"github.com/chicken-team-outside/chicken_transmission/proxy_server/middleware"
	rpc_service "github.com/chicken-team-outside/chicken_transmission/proxy_server/rpc"
	"github.com/chicken-team-outside/chicken_transmission/proxy_server/setting"
	"github.com/chicken-team-outside/chicken_transmission/proxy_server/web"
	ginzap "github.com/gin-contrib/zap"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"time"
)

func Run() error {
	gin.SetMode(setting.ServerMode)
	engine := gin.New()
	engine.Use(ginzap.Ginzap(zap.L(), time.RFC3339, true))
	engine.Use(ginzap.RecoveryWithZap(zap.L(), true))
	web.InitWebResource(engine)
	apiGroup := engine.Group("/api")
	{
		apiV1Group := apiGroup.Group("/v1")
		{
			initV1Captcha(apiV1Group.Group("/captcha"))
			initV1User(apiV1Group.Group("/user"))
			initTransport(apiV1Group.Group("/transport"))
			rpc_service.Init(apiV1Group.Group("/rpc", middleware.RpcAuthWare))
		}
	}
	err := engine.Run(setting.ServerAddress)
	if err != nil {
		return fmt.Errorf("failed to initialize engine: %w", err)
	}
	return nil
}
